<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="60%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" :colon="true" :data="formData" labelWidth="100px" :rules="rules">
        <t-form-item label="标题" name="title">
          <t-input clearable v-model="formData.title" placeholder="请输入标题"></t-input>
        </t-form-item>
        <t-form-item label="附件上传" name="sysAttMainVo.sysAttMainIds">
          <file-upload :fileList="fileLists" v-model="formData.sysAttMainVo.sysAttMainIds"></file-upload>
        </t-form-item>
        <t-form-item label="显示排序" name="sort">
          <t-input-number clearable v-model="formData.sort" :min="1" placeholder="请输入" />
        </t-form-item>
        <t-form-item label="是否显示" name="isShow">
          <t-switch
            size="large"
            v-model="formData.isShow"
            :label="['是', '否']"
            :customValue="['1', '0']"
          ></t-switch>
        </t-form-item>
        <t-form-item label="备注" name="content">
          <t-textarea v-model="formData.content" :autosize="{minRows: 5}"/>
        </t-form-item>

      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";

export default {
  name: "add-edit-help-doc",
  components:{
    FileUpload
  },
  props: ['header', 'objId'],
  data(){
    return {
      visible: true,
      loading: false,
      fileLists: [],
      formData:{
        title: '',
        content: '',
        sort: 1,
        sysAttMainVo:{
          sysAttMainIds:[]
        },
        isShow: '1'
      },
      rules:{
        title: [{ required: true, message: "标题不能为空"}],
        'sysAttMainVo.sysAttMainIds': [{required: true, validator: (val) => val && val.length, message: "附件不能为空", type: "error"}],
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  methods:{
    getObj() {
      this.$api.system.help.get(this.objId).then(response => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || {sysAttMainIds:[]}
        Object.assign(this.formData, response.data)
        getFiles(this.objId).then((res) => {
          this.fileLists = []
          const ids = [];
          if (res.data && res.data.length) {
            res.data.forEach((item) => {
              ids.push(item.id);
              this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
            })
            this.formData.sysAttMainVo.sysAttMainIds = ids;
          }
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = false;
          if (this.objId) {
            this.$api.system.help.update(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.help.add(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
